package com.edu.smsys.service.impl;

import com.edu.smsys.dao.entity.TeacherEntity;
import com.edu.smsys.dao.mapper.TeacherEntityMapper;
import com.edu.smsys.model.form.TeacherForm;
import com.edu.smsys.model.form.TeacherQueryForm;
import com.edu.smsys.model.vo.TeacherVO;
import com.edu.smsys.service.BaseService;
import com.edu.smsys.util.Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 老师管理逻辑类
 */
@Service
public class TeacherService implements BaseService<TeacherEntity> {

    @Autowired
    private TeacherEntityMapper teacherEntityMapper;
    @Autowired
    private UserService userService;

    /**
     * 统计老师个数
     * @return
     */
    public int selectCount(){
        return teacherEntityMapper.selectCount();
    }

    /**
     * 查询所有所有老师
     * @return
     */
    @Override
    public List<TeacherEntity> findAll() {
        return teacherEntityMapper.queryTeaChers();
    }

    /**
     * 查询老师根据老师id
     * @param id
     * @return
     */
    @Override
    public TeacherEntity findEntityById(int id) {
        return teacherEntityMapper.queryTeaCherById(id);
    }

    /**
     * 查询老师根据老师实体 返回一个list
     * @return
     */
    @Override
    public List<TeacherEntity> findEntity(TeacherEntity entity) {
        return teacherEntityMapper.queryTeacherByTeacher(entity);
    }
    public List<TeacherVO> findEntity(TeacherQueryForm form) {
        List<TeacherVO> voList=new ArrayList<>();
        TeacherEntity entity=new TeacherEntity();
        //设置查询名称
        if (Util.isNull(form.getTeacherName())){
            entity.setTeacherName(form.getTeacherName());
        }
        //设置查询编号
        if (Util.isNull(form.getTeacherCode())){
            entity.setTeacherCode(form.getTeacherCode());
        }
        //设置查询入职时间
        String dateStr=form.getTeacherStartTime();
        if (dateStr!=null&&!dateStr.equals("")){
            Date teacherStartTime= Util.str2Date(form.getTeacherStartTime(),"yyyy-MM-dd");//将入职时间字符串转换为java时间对象
            entity.setTeacherStartTime(teacherStartTime);
        }
        //获得查询结果
        List<TeacherEntity> queryTeacherByTeacher= teacherEntityMapper.queryTeacherByTeacher(entity);
        if (queryTeacherByTeacher!=null&&queryTeacherByTeacher.size()>0){
            for (TeacherEntity e:queryTeacherByTeacher){
                TeacherVO vo=new TeacherVO();
                entitie2VO(e,vo);
                voList.add(vo);
            }
        }else{//如果为空返回全部查询结果
            return findAllVO();
        }
        return voList;
    }

    /**
     * 修改老师信息
     * @param entity
     * @return
     */
    @Override
    public int updateEntity(TeacherEntity entity) {
        return teacherEntityMapper.updateTeacherById(entity);
    }
    /**
     * 修改老师信息
     * @param form
     * @return
     */
    public int updateEntity(TeacherForm form) {
        TeacherEntity entity=new TeacherEntity();
        Date teacherStartTime= Util.str2Date(form.getTeacherStartTime(),"yyyy-MM-dd");//将入职时间字符串转换为java时间对象
        entity.setId(form.getId());
        entity.setTeacherStartTime( teacherStartTime);//设置入职时间
        entity.setTeacherAddress(form.getTeacherAddress());//教师地址
        entity.setUserId(form.getUserId());//绑定用户id
        entity.setTeacherCode(form.getTeacherCode());//老师编号
        entity.setTeacherSalary(form.getTeacherSalary());//教师工资
        entity.setTelephone(form.getTelephone());//电话
        entity.setTeacherName(form.getTeacherName());//老师名称
        return teacherEntityMapper.updateTeacherById(entity);
    }

    /**
     * 插入老师信息
     * @param entity
     * @return
     */
    @Override
    public int addEntity(TeacherEntity entity) {
        return teacherEntityMapper.insertTeacher(entity);
    }

    /**
     * 插入老师信息
     * @param form
     * @return
     */

    public int addEntity(TeacherForm form) {
        TeacherEntity entity=new TeacherEntity();
        Date teacherStartTime= Util.str2Date(form.getTeacherStartTime(),"yyyy-MM-dd");//将入职时间字符串转换为java时间对象
        entity.setTeacherStartTime( teacherStartTime);//设置入职时间
        entity.setTeacherAddress(form.getTeacherAddress());//教师地址
        entity.setUserId(form.getUserId());//绑定用户id
        entity.setTeacherCode(form.getTeacherCode());//老师编号
        entity.setImgSrc(form.getImgSrc());//头像路径
        entity.setTeacherSalary(form.getTeacherSalary());//教师工资
        entity.setTelephone(form.getTelephone());//电话
        entity.setSubjectId(form.getSubjectId());//学科id
        entity.setTeacherName(form.getTeacherName());//老师名称
        return teacherEntityMapper.insertTeacher(entity);
    }
    /**
     * 删除老师根据id
     * @param id
     * @return
     */
    @Override
    public int deleteEntity(int id) {
        return teacherEntityMapper.deleteTeacherById(id);
    }

    public List<TeacherVO> findAllVO() {
        List<TeacherVO> voList=new ArrayList<>();
        List<TeacherEntity> entities = findAll();
        for (TeacherEntity entity:entities ){
            TeacherVO vo=new TeacherVO();
            entitie2VO(entity,vo);
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 实体与视图对象转换
     * @param entity
     * @param vo
     */
    private void entitie2VO(TeacherEntity entity, TeacherVO vo) {
        BeanUtils.copyProperties(entity,vo,"userId","subjectId");
        vo.setUser(userService.findUserById(entity.getUserId()));
    }


}
